#include "Game.h"

Engine::Engine()
{
	for(int i(0); i < ZONE_SIZE;i++)
		m_bInfectedZones[i] = false;
	m_bGameOver = false;
	m_iInfZFlag = -1;
	CountriesForsaken = 0;
	CountriesVictorious = 0;
	m_bInfectedZones[ZONE_MARIANIYAH] = true; 
}
//=====STARTUP
void Engine::Initialize(HWND hWnd,HINSTANCE hInstance)
{
	srand((int)time(0));		

	//===INITIALIZE VARIABLES
	DebugBox		= PrintBox(0, 100, 0 , 50, "DEBUG");
	MousePosition	= PrintBox(0, 100, 25, 50, "MOUSE");	
	NewsStream1 = NewsStream2 = NewsStream3 = NewsStream4 = NewsStream5 = "";

	//===CALL OTHER INIT FUNCTIONS
	Graphics::Into()->Initialize(hWnd);			
	Input::Into()->Initialize(hWnd,hInstance);	
	SoundEngine::Into()->SoundInit();
	SoundEngine::Into()->SoundCreate();
	SoundEngine::Into()->SoundStream();
}
//=====INITIALIZATION
void Engine::InitializeButtonList(L_Button B_List)
{
	for each (Button* button in B_List)
		button->Initialize(Graphics::Into()->GetTexture(TEX_BUTTON));
}
void Engine::InitializeMenuList(L_Menu List)
{
	for each(MenuPanel* M in List)
		M->Initialize(Graphics::Into()->GetTexture(TEX_BUTTON));
}
//=====UPDATE
void Engine::UpdateEventPane(MenuPanel* EventPane)
{
	stringstream AllText ;
	AllText << NewsStream1;
	AllText << NewsStream2;
	AllText << NewsStream3;
	AllText << NewsStream4;
	AllText << NewsStream5;
	EventPane->ModifyContent(AllText.str());	
}
void Engine::UpdateObjects(L_Zone ZList,L_Object OList, L_Button BList, L_Menu MList)
{
	std::stringstream oss;
	oss.str("");
	oss << "  Infectivity: "  << Pathogen::Into()->iGetVir();
	oss << "  Lethality:   "  << Pathogen::Into()->CheckLethality();
	oss << "  Visibility:  "  << Pathogen::Into()->CheckVisibility();
	oss << "  Points:  "	  << Pathogen::Into()->CheckMutationPoints();
	MousePosition.Content = oss.str();

	if(Timer::Into()->bQueryDeltaT(100))
	{
		SoundEngine::Into()->SoundUpdate();

		UpdateZones(ZList);			
		UpdateMenus(MList);
		UpdateButtons(BList,MList);
		CheckIfGameOver(ZList);
	}
};
void Engine::UpdateZones(L_Zone List)
{
	if(c_eiGameState == GS_GAMEPLAY){						//If: We're in play
		m_iTotalHealthy  = 0;
		m_iTotalInfected = 0;
		m_iTotalDeceased = 0;
		for each (Zone*  Region in List){
			Region->Update(m_iTotalHealthy,m_iTotalInfected,m_iTotalDeceased);
			if(m_iInfZFlag >= 0)									//If something got infected
				if(!List[m_iInfZFlag]->isInfected())				//If it wasn't already infected
				{
					List[m_iInfZFlag]->BecomeInfected();				//Tell it it's infected	
					PostInfectedMessage(List[m_iInfZFlag]->GetName());	//Post the message
				}//End:if(!List[m_iInfZFlag]->isInfected())	
		}//End:for each (Zone*  Region in List)
	}
}
void Engine::UpdateMenus(L_Menu MList)
{
	for each(MenuPanel* toUpdate in MList)						
	{
		toUpdate->Update();
		if(toUpdate->MenuTypeID() == -2)
			UpdateEventPane(toUpdate);
	}
}
void Engine::UpdateButtons(L_Button BList,L_Menu MList)
{
	for(int i(0),iManifest(0); i < BList.size(); i++)			//=====UPDATE BUTTON
	{
		BList[i]->Update();
		(BList[i]->isType(B_PURCHASE)?iManifest++:NULL);
		if(Input::Into()->isClicked(LEFT))
			if(BList[i]->isActive())
				if(Input::Into()->isWithin(BList[i]->GetRect()))	
				{
					SoundEngine::Into()->SoundPlay(sClick);
					switch(BList[i]->GetType())
					{
					case B_PURCHASE:	Pathogen::Into()->BuyManifest(iManifest);	break;
					case B_ZONES:		MList[i]->SetState(true);					break;
					}
					c_eiGameState = BList[i]->m_eiStateTrigger;
				}
	}//Endfor
}
void Engine::CheckIfGameOver(L_Zone ZList)
{
	if(!m_bGameOver)
	{
		if(CountriesForsaken == ZONE_SIZE)
		{
			string WinMessage = "The last survivor gasps his last breath...\n What once was a lush paradise is now an infinite tomb...\n Darkness descends. \n	You have won.\n\n";
			ModifyEventPane(WinMessage);
			m_bGameOver = true;
		}
		if(m_iTotalInfected == 0 && !m_bGameOver)
		{
			string WinMessage = "Jaded to disease. \nUnperturbed by your catastrope.\nSurviors remain to defy you...\n	You have lost.\n\n";
			ModifyEventPane(WinMessage);
			m_bGameOver = true;
		}
	}
}
//=====RENDER
void Engine::Render(L_Zone ZList, L_Object OList, L_Button BList, L_Menu MList)
{
	Graphics::Into()->Render(ZList,OList,BList,MList,MousePosition);
}
//=====MUTATORS
void Engine::InfectNewZone(int zone)
{
	m_bInfectedZones[zone]	= true;
	m_iInfZFlag	= zone;
}
void Engine::DisinfectZone(int zone)
{
	m_bInfectedZones[zone] = false;
}
//=====EVENT PANE
void Engine::ModifyEventPane(string NewText)
{
	NewsStream5 = NewsStream4;
	NewsStream4 = NewsStream3;
	NewsStream3 = NewsStream2;
	NewsStream2 = NewsStream1;
	NewsStream1 = NewText;
}
void Engine::PostInfectedMessage(string name)
{
	EventMessage.str("");
	EventMessage << name;
	EventMessage << " has been infiltrated by infection.\n\n";
	ModifyEventPane(EventMessage.str());
	m_iInfZFlag = -1;			//Reset the messageflag.
	Pathogen::Into()->AddMutationPts(3);
}
void Engine::PostCrippledMessage(string name)
{
	EventMessage.str("");
	EventMessage << name;										//Load the OSS with the news
	EventMessage << " has been crippled by the Pathogen.\n\n";	//Load the OSS with the news
	ModifyEventPane(EventMessage.str());						//Pass the OSS to the Event Pane
	Pathogen::Into()->AddMutationPts(2);
}
void Engine::PostForsakenMessage(string name)
{
	EventMessage.str("");
	EventMessage << name;							//Now we tell the world
	EventMessage << " is completely void of life.\n\n";			//This country has been lost
	ModifyEventPane(EventMessage.str());						//And we pass it to the event pane
	CountriesForsaken++;
	Pathogen::Into()->AddMutationPts(3);
}
void Engine::PostVaccinatMessage(string name)
{
	EventMessage.str("");
	EventMessage << name;							
	EventMessage << " has begun curing infected citizens.\n\n";				
	ModifyEventPane(EventMessage.str());	
}
void Engine::PostResistorMessage(string name)
{
	EventMessage.str("");
	EventMessage << name;							
	EventMessage << " has begun killing infected citizens.\n\n";				
	ModifyEventPane(EventMessage.str());	
}
void Engine::PostExterminMessage(string name)
{
	EventMessage.str("");
	EventMessage << name;							
	EventMessage << " has fought the infection off.\n\n";				
	ModifyEventPane(EventMessage.str());
	CountriesVictorious++;
}
//=====DEBUG
PrintBox::PrintBox(float L, float R, float T, float B, string Txt){

	Location.left	= (long)L;						
	Location.right	= (long)R;					
	Location.top	= (long)T;
	Location.bottom = (long)B;
	Content = Txt;
}
void PrintBox::Draw()
{
	Graphics::Into()->GetFDevice()->DrawTextA
		(NULL,Content.c_str(),
		Content.size(),&Location,
		DT_LEFT | DT_NOCLIP,D3DCOLOR_ARGB(255, 255, 255, 255));
}
void Engine::MoveObject(Drawable* tomove)
{

	switch(Input::Into()->AquireInput())
	{
	case DIR_UP:		tomove->m_vecPosition.y-=1;		break;
	case DIR_DOWN:		tomove->m_vecPosition.y+=1;		break;
	case DIR_LEFT:		tomove->m_vecPosition.x-=1;		break;
	case DIR_RIGHT:		tomove->m_vecPosition.x+=1;		break;
	case DIR_SHEIGHTU:	tomove->m_vecScale.y +=0.01f;	break;
	case DIR_SHEIGHTD:	tomove->m_vecScale.y -=0.01f;	break;
	case DIR_SWIDTHU:	tomove->m_vecScale.x +=0.01f;	break;
	case DIR_SWIDTHD:	tomove->m_vecScale.x -=0.01f;	break;
	case DIR_SALLU:		tomove->m_vecScale.x +=.01f;	tomove->m_vecScale.y +=.01f;		break;
	case DIR_AALLD:		tomove->m_vecScale.x -=.01f;	tomove->m_vecScale.y -=.01f;		break;
	}

	std::stringstream OSS;
	float xLoc = tomove->m_vecPosition.x;
	float yLoc = tomove->m_vecPosition.y;
	float xSca = tomove->m_vecScale.x;
	float YSca = tomove->m_vecScale.y;

	OSS << "Xl: " << xLoc << endl;
	OSS << "Yl: " << yLoc << endl;
	OSS << "Xs: " << xSca << endl;
	OSS << "Ys: " << YSca << endl;
	DebugBox.Content = OSS.str();

}
void Engine::ChangeDebugBoxText(string NewText)
{
	//This function is purely for debug purposes
	DebugBox.Content = NewText;
}
void Engine::ChangeDebugBoxTextToNumber(float Num)
{
	std::stringstream OSS;
	OSS << Num << endl;
	DebugBox.Content = OSS.str();
}
